package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.OrderInforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderInfoController {
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private OrderInforService orderInforService;
    @RequestMapping("/addUser")
    public Result addUser(@RequestBody Map map) {
        try {
            //获取缓存中的验证码信息
            String jedisCode = jedisPool.getResource().get(map.get("telephone") + RedisMessageConstant.SENDTYPE_ORDER);
            //获取验证码
            String validateCode = (String) map.get("validateCode");
            //判断验证码是否输入正确
            if (jedisCode!=null&&validateCode!=null&&jedisCode.equals(validateCode)) {
                Result result = orderInforService.addUser(map);
                //发送短信提醒预约成功
                //SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,(String) map.get("telephone"), (String) map.get("orderDate"));
                return result;
            } else {
                return new Result(false, MessageConstant.VALIDATECODE_ERROR);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "预约失败");
        }
    }

    @RequestMapping("/findById")
    public Result findById(int id){
        return orderInforService.findById(id);
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        return orderInforService.findAll();
    }
}
